package cn.edu.xmu.oomall.payment.dao;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.payment.mapper.ErrorAccountPoMapper;
import cn.edu.xmu.oomall.payment.model.bo.ErrorAccount;
import cn.edu.xmu.oomall.payment.model.po.ErrorAccountPo;
import cn.edu.xmu.privilegegateway.annotation.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Repository
public class ErrorAccountDao {
    @Autowired
    private ErrorAccountPoMapper errorAccountPoMapper;

    /**
     * 增加错账单
     */
    public ReturnObject insertErrorAccount(ErrorAccount errorAccount){
        ErrorAccountPo errorAccountPo= Common.cloneVo(errorAccount,ErrorAccountPo.class);
        try{
            int ret=errorAccountPoMapper.insert(errorAccountPo);
            if(ret==0){
                return new ReturnObject(ReturnNo.FIELD_NOTVALID);
            }
            return new ReturnObject(errorAccountPo);
        }catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 修改
     */
    public ReturnObject updateErrorAccount(ErrorAccount errorAccount){
        ErrorAccountPo errorAccountPo=Common.cloneVo(errorAccount,ErrorAccountPo.class);
        try{
            int ret=errorAccountPoMapper.updateByPrimaryKey(errorAccountPo);
            if(ret==0){
                return new ReturnObject(ReturnNo.FIELD_NOTVALID);
            }
            return new ReturnObject(ReturnNo.OK);
        }catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 删除错账单
     */
    public ReturnObject deleteErrorAccount(Long id){
        try{
            int ret=errorAccountPoMapper.deleteByPrimaryKey(id);
            if(ret==0){
                return new ReturnObject(ReturnNo.FIELD_NOTVALID);
            }
            return new ReturnObject(ReturnNo.OK);
        } catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 根据id查看错账单
     */
    public ReturnObject selectErrorAccountById(Long id){
        try{
            ErrorAccountPo errorAccountPo=errorAccountPoMapper.selectByPrimaryKey(id);
            return new ReturnObject(Common.cloneVo(errorAccountPo,ErrorAccount.class));
        } catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }
}
